package com.aries.ui.widget.demo.module.tab.adapter;

import com.aries.ui.widget.demo.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

public class SimpleHomeAdapter extends BaseItemProvider {
    private final Context context;
    private String[] mItems = null;
    private final LayoutScatter layoutScatter;

    public SimpleHomeAdapter(Context context, String[] mItems) {
        this.context=context;
        if(mItems!=null){
            this.mItems = mItems.clone();
        }
        this.layoutScatter= LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return mItems.length;
    }

    @Override
    public String getItem(int i) {
        return mItems[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        //component相当于view，其他的和ListView的适配器adapter差不多。
        // 名字区别也不大，不过ListView基本被淘汰了。
        if (component == null) {
            component = layoutScatter.parse(ResourceTable.Layout_item_simple_layout, null, false);
            viewHolder = new ViewHolder((ComponentContainer) component);
            component.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        viewHolder.tvItemName.setText(mItems[i]);
        return component;
    }
    static class ViewHolder {
        Text tvItemName;
        public ViewHolder(ComponentContainer componentContainer) {
            tvItemName = (Text) componentContainer.findComponentById(ResourceTable.Id_tv_item);
        }
    }
}
